package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.PropertyFee;
import com.fz.mapper.PropertyFeeMapper;
import com.fz.service.PropertyFeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.vo.PayVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fz
 * @since 2024-08-21
 */
@Service
public class PropertyFeeServiceImpl extends ServiceImpl<PropertyFeeMapper, PropertyFee> implements PropertyFeeService {
    @Resource
    PropertyFeeMapper propertyFeeMapper;
    @Override
    public PayVo getUsernameById(Integer id,Long userId){
        PayVo payVo=propertyFeeMapper.getUsernameById(id,userId);
        return payVo;
    }
    @Override
    public List<PayVo> setPayVoList(List<PropertyFee> propertyFeeList){
        List<PayVo> payVoList1=new ArrayList<>();
        for(PropertyFee propertyFee:propertyFeeList){
            Long userid = Long.valueOf(propertyFee.getUserId());
            //userId.add(userid);
            Integer id= Math.toIntExact(propertyFee.getId());
           // Id.add(id);
            payVoList1.add(this.getUsernameById(id,userid));
        }

        return payVoList1;
    }
    @Override
    public boolean check(Long id,String field,String value)
    {
        QueryWrapper<PropertyFee> wrapper=new QueryWrapper<>();
        wrapper.eq(field,value);
        System.out.println(id);
        if(null!=id)
        {
            wrapper.ne("id",id);
        }
        return this.count(wrapper)==0;
    }
    @Override
    public Page<PropertyFee> getList(Long userId, Integer pageNo, Integer pageSize) {
        QueryWrapper<PropertyFee> wrapper = new QueryWrapper<>();
        Page<PropertyFee>propertyFeePage;
        wrapper.eq("user_id", userId)
                .orderByDesc("payment_time");
        propertyFeePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return propertyFeePage;
    }
    @Override
    public Page<PropertyFee> showList(Integer pageNo, Integer pageSize){
        QueryWrapper<PropertyFee> wrapper=new QueryWrapper<>();
        Page<PropertyFee> propertyFeePage;
        wrapper.orderByDesc("payment_time");
        propertyFeePage=this.page(new Page<>(pageNo,pageSize),wrapper);
        return  propertyFeePage;

    }

    @Override
    public boolean checktime(Integer userId){
        YearMonth currentYearMonth=YearMonth.now();
        QueryWrapper<PropertyFee>wrapper=new QueryWrapper<>();
        wrapper.eq("year(payment_time)", currentYearMonth.getYear())
                .eq("month(payment_time)", currentYearMonth.getMonthValue())
                .eq("user_id", userId);
       // System.out.println(this.count(wrapper)==0);
        return this.count(wrapper)==0;
    }

}
